//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package wudashan;

import java.net.InetSocketAddress;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.core.server.resources.Resource;
import org.eclipse.californium.scandium.DTLSConnector;
import org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder;
import org.eclipse.californium.scandium.dtls.cipher.CipherSuite;
import org.eclipse.californium.scandium.dtls.pskstore.PskStore;

public class SimulatorServer extends CoapServer {
    private CoapResource resource;
    private EDRXSimulator edrxSimulator;

    public SimulatorServer(CoapResource resource, EDRXSimulator edrxSimulator) {
        this.resource = resource;
        this.edrxSimulator = edrxSimulator;
    }

    public void startServer() {
        this.getEndpoints().clear();
        this.resource.clearObserveRelations();
        if (this.edrxSimulator.isUseSecure()) {
            Builder builder = new Builder(new InetSocketAddress(0));
            builder.setPskStore(new SimulatorServer.TupClientPskStore());
            builder.setSupportedCipherSuites(new CipherSuite[]{CipherSuite.TLS_PSK_WITH_AES_128_CCM_8, CipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA256});
            DTLSConnector connector = new DTLSConnector(builder.build());
            this.addEndpoint(new CoapEndpoint(connector, NetworkConfig.getStandard()));
        } else {
            this.addEndpoint(new CoapEndpoint());
        }

        this.add((new RootResource("t")).add(this.resource));
        this.start();
        InetSocketAddress inetSocketAddress = ((Endpoint)this.resource.getEndpoints().get(0)).getAddress();
        this.edrxSimulator.getLogTextArea().printLog("UE模拟器开启服务端，IP：%s， PORT：%s", inetSocketAddress.getAddress().getHostAddress(), inetSocketAddress.getPort());
    }

    private static synchronized byte[] parseHexStr2Byte(String hexStr) {
        hexStr = hexStr.replaceAll(" ", "");
        if (hexStr.length() < 1) {
            return null;
        } else {
            if (hexStr.length() % 2 != 0) {
                hexStr = "0" + hexStr;
            }

            byte[] result = new byte[hexStr.length() / 2];

            for(int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte)(high * 16 + low);
            }

            return result;
        }
    }

    public class TupClientPskStore implements PskStore {
        public TupClientPskStore() {
        }

        public byte[] getKey(String identity) {
            return SimulatorServer.parseHexStr2Byte(SimulatorServer.this.edrxSimulator.getPskTextField().getText());
        }

        public String getIdentity(InetSocketAddress inetAddress) {
            return SimulatorServer.this.edrxSimulator.getEpTextField().getText();
        }
    }
}
